package main

import (
	"fmt"
	"math/rand"
	"time"

	//"fmt"
	sago "gitee.com/xiawucha365/sago.v2"
	"gitee.com/xiawucha365/sago.v2/core/config"
	slog "gitee.com/xiawucha365/sago.v2/core/log"
	sagoutil "gitee.com/xiawucha365/sago.v2/core/util"
	"gitee.com/xiawucha365/sago.v2/core/orm"
	"gopkg.in/mgo.v2/bson"
	"gitee.com/xiawucha365/sago.v2/parallel"
)

type Demo struct {
	Id       string
	Username string
	Pdate    string
}

type Demo2 struct {
	Id   string
	Uid1 string
}

type Item2 struct {
	Pid   string
	Ptype string
}

type Std struct {
	Pid   string
	Title string
}


/*
运行方式
go run main.go -conf="/Users/mfw/Documents/data/go/src/sago.v2/examples/etc/dev.toml"
go build main.go && ./main
*/
func main() {

	//配置打印
	sago.Gdi.Invoke(func(cf *config.Config) {
		sagoutil.PrintType(cf)
	})

	//日志使用
	sago.Gdi.Invoke(func(log *slog.Log) {

		defer  log.Sync()
		log.Info("log测试")
		log.Warn("log测试")
		log.Error("log测试")
	})

	//mysql=============================================================================================================
	//mysql 框架本身查询语句
	sago.Gdi.Invoke(func(db *orm.Db) {
		var demos []Demo
		_ = db.FindBySql("select * from t_creator_center  limit 2", &demos)
		fmt.Println("xorm 框架查询")
		sagoutil.PrintType(demos)
	})

	//mysql xorm引擎 原生查询语句
	sago.Gdi.Invoke(func(db *orm.DbMysql) {
		var demos []Demo
		res, err := db.Engine.Xorm.QueryString("select * from t_creator_center  limit 2")
		if err != nil {
			panic(err)
		} else {
			if res1, err := sagoutil.JsonEncode(res); err != nil {
				panic(err)
			} else {
				_ = sagoutil.JsonDecode(res1, &demos)
			}
		}
		fmt.Println("xorm 原生查询")
		sagoutil.PrintType(demos)
	})

	//mysql gorm引擎 原生查询语句 需要修改配置文件mysql-engine为gorm

	//sago.Gdi.Invoke(func(db sago.GDbParams) {
	//
	//	var demos []Demo
	//	res := db.Mysql.Engine.Gorm.Raw("select * from t_creator_center  limit 2").Find(&demos)
	//	if res.Error != nil {
	//		panic(res.Error)
	//	} else {
	//		fmt.Println("gorm 原生查询")
	//		sagoutil.PrintType(demos)
	//	}
	//})

	//指定数据库
	sago.Gdi.Invoke(func(cf *config.Config) {

		db := orm.GetMysqlConnByDb(cf, "gorm", "sale_contact")
		var demos []Demo2
		_ = db.FindBySql("select * from t_hm_friend  limit 2", &demos)
		fmt.Println("指定数据库查询")
		sagoutil.PrintType(demos)

	})

	//使用addr查询
	sago.Gdi.Invoke(func(cf *config.Config) {

		db := orm.GetMysqlConnByAddr(cf, "",
			"mfw_dev:2cB509cc0bf@tcp(172.18.12.126:3376)/sale_spider?charset=utf8&parseTime=True&loc=Local")
		var demos []Demo
		_ = db.FindBySql("select * from t_creator_center  limit 2", &demos)
		fmt.Println("指定ddr查询")
		sagoutil.PrintType(demos)
	})

	//mongodb================================================
	//框架查询语句-待封装

	//mgo 原生查询
	sago.Gdi.Invoke(func(db *orm.DbMongodb) {
		var demo Item2
		if err := db.Engine.Mgo.C("video_info").Find(bson.M{"ptype": "YD"}).One(&demo); err != nil {
			panic(err)
		} else {
			fmt.Println("mongo查询")
			sagoutil.PrintType(demo)
		}
	})


	//并发例子

	var items []int
	for i:=1;i<=100;i++ {
		items = append(items,i)
	}

	sp := parallel.NewSPool(5, len(items),0,false)

	for _,id:= range items {
		np := worker{ID: id}
		sp.Commit(&np)
	}

	sp.Runtimelog()
	sp.Release()

}
type worker struct {
	ID int
}
func (m *worker) Task() error {

	fmt.Println("job:" , m.ID , "runing...")
	timen := rand.Intn(3)
	//fmt.Println(timen,"seconds")
	time.Sleep(time.Second * time.Duration(timen))
	fmt.Println("job:" , m.ID , "over")
	return nil
}


//获取任务id,便于
func (m *worker) GetTaskID() interface{} {
	return m.ID
}


func StringSliceEqual(a, b []string) bool {
	if len(a) != len(b) {
		return false
	}

	if (a == nil) != (b == nil) {
		return false
	}

	for i, v := range a {
		if v != b[i] {
			return false
		}
	}
	return true
}